//
//  Body.m
//  TapEnglish
//
//  Created by 이선동 on 11. 4. 13..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Body.h"
#import "TapEnglishAppDelegate.h"

@implementation Body

NSString *currentEngWord;

- (TapEnglishAppDelegate *)appDelegate {
    return [[UIApplication sharedApplication] delegate];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [labelCaption release];
    [labelPinyin release];
    [labelEnglish release];
    [segmentFlag release];
    [button_head release];
    [button_hand release];
    [button_arm release];
    [button_leg release];
    [button_foot release];
    [button_face release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    currentEngWord = @"body";
    if (segmentFlag.selectedSegmentIndex == 0) {
        [self working:currentEngWord];
    } 
    [segmentFlag setSelectedSegmentIndex:[[self appDelegate].nationCode intValue]];
   
}

- (void)viewDidUnload
{
    [labelCaption release];
    labelCaption = nil;
    [labelPinyin release];
    labelPinyin = nil;
    [labelEnglish release];
    labelEnglish = nil;
    [segmentFlag release];
    segmentFlag = nil;
    [button_head release];
    button_head = nil;
    [button_hand release];
    button_hand = nil;
    [button_arm release];
    button_arm = nil;
    [button_leg release];
    button_leg = nil;
    [button_foot release];
    button_foot = nil;
    [button_face release];
    button_face = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)goHome:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)speakerTapped:(id)sender {
    [self working:currentEngWord];
}

- (IBAction)button_head_tapped:(id)sender {
    [self working:@"head"];
    [self buttonColorReset];
    [button_head setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_hand_tapped:(id)sender {
    [self working:@"hand"];
    [self buttonColorReset];
    [button_hand setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_arm_tapped:(id)sender {
    [self working:@"arm"];
    [self buttonColorReset];
    [button_arm setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_leg_tapped:(id)sender {
    [self working:@"leg"];
    [self buttonColorReset];
    [button_leg setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_foot_tapped:(id)sender {
    [self working:@"foot"];
    [self buttonColorReset];
    [button_foot setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_face_tapped:(id)sender {
    [self working:@"face"];
    [self buttonColorReset];
    [button_face setBackgroundColor:[UIColor blueColor]];
}

- (void) working:(NSString *)engWord {
    currentEngWord=engWord;

    NSArray *imsiArray = [[NSArray alloc] initWithArray:[[self appDelegate].vocaGroup valueForKey:engWord]];

    switch ([[self appDelegate].nationCode intValue]) {
        case 0: //영어
            labelCaption.text=[imsiArray objectAtIndex:0];
            labelPinyin.text=@"";
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"ENG_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 1: //중국어
            labelCaption.text=[imsiArray objectAtIndex:1];
            labelPinyin.text=[imsiArray objectAtIndex:2];
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"CHA_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 2: //일본어
            labelCaption.text=[imsiArray objectAtIndex:3];
            labelPinyin.text=[imsiArray objectAtIndex:4];
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"JPN_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 3: //한국어
            labelCaption.text=[imsiArray objectAtIndex:5];
            labelPinyin.text=@"";
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"KOR_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        default:
            NSLog(@"Unknow nationCode");
            break;
    }
    [imsiArray release];
}

- (IBAction)segmentNation_tapped:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    [self appDelegate].nationCode = [NSNumber numberWithInt:segmentedControl.selectedSegmentIndex];
    [self working:currentEngWord];
}

- (void) buttonColorReset {
    [button_head setBackgroundColor:[UIColor whiteColor]];
    [button_hand setBackgroundColor:[UIColor whiteColor]];
    [button_arm setBackgroundColor:[UIColor whiteColor]];
    [button_leg setBackgroundColor:[UIColor whiteColor]];
    [button_foot setBackgroundColor:[UIColor whiteColor]];
    [button_face setBackgroundColor:[UIColor whiteColor]];
}


@end
